Day 3: Perfectly Spherical Houses in a Vacuum

Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

> delivers presents to 2 houses: one at the starting location, and one to the east.
^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.

In [8]:
def next_house(position, move):
    x, y = position
    if move == '^':
        return x+1, y
    if move == 'v':
        return x-1, y
    if move == '>':
        return x, y+1
    if move == '<':
        return x, y-1
    raise RuntimeError("Unknown move: '{}'".format(move))
    
def visited_houses(instructions):
    position = (0, 0)
    houses = set()
    houses.add(position)
    for move in instructions:
        position = next_house(position, move)
        houses.add(position)
    return houses

def count_houses(instructions):
    return len(visited_houses(instructions))

In [9]:
# Test examples
count_houses("^v^v^v^v^v")


Out[9]:
2

In [10]:
# Real puzzle
with open('day3/input.txt', 'rt') as fd:
    print("Santa will visit {} houses.".format(count_houses(fd.read())))


Santa will visit 2081 houses.

Part Two

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.

In [16]:
def count_houses_with_robo(instructions):
    santa_instructions = [instructions[i] for i in range(0, len(instructions), 2)]
    robo_instructions = [instructions[i] for i in range(1, len(instructions), 2)]
    
    houses_santa = visited_houses(santa_instructions)
    houses_robo = visited_houses(robo_instructions)
    
    houses = houses_santa.union(houses_robo)    
    return len(houses)

In [20]:
# Test example
count_houses_with_robo('^v^v^v^v^v')


Out[20]:
11

In [21]:
# Real puzzle
with open('day3/input.txt', 'rt') as fd:
    print("Santa and Robo will visit {} houses.".format(count_houses_with_robo(fd.read())))


Santa and Robo will visit 2341 houses.